﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Utils
{
    public class PrizeHelper
    {
        private static Random _random = new Random();

        public static bool PrizeDraw(int probability)
        {
            if (probability == 0) return false;
            if (probability >= 100) return true;
            return _random.Next(100) < probability;
        }
        /// <summary>
        /// 抽奖
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <param name="drawCount"></param>
        /// <returns></returns>
        public static T[] PrizeDraw<T>(IEnumerable<T> source, int drawCount)
        {
            return PrizeDraw<T>(source, drawCount, null);
        }

        /// <summary>
        /// 抽奖
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <param name="drawCount"></param>
        /// <param name="probabilityAmplification">概率放大</param>
        /// <returns></returns>
        public static T[] PrizeDraw<T>(IEnumerable<T> source, int drawCount, Dictionary<int, double> probabilityAmplification)
        {
            drawCount = Math.Min(drawCount, source.Count());
            var r = new Random();
            return source.Select((a, i) => new
            {
                item = a,
                radix = r.NextDouble() * (probabilityAmplification?.TryGetValue(i, 1) ?? 1)
            })
                .OrderByDescending(a => a.radix)
                  .Select(a => a.item)
                  .Take(drawCount)
                  .ToArray();
        }
    }
}
